package org.ciallo.easytokencore.exception.handler;

import org.ciallo.easytokencore.exception.EasyLoginException;
import org.ciallo.easytokencore.exception.EasyTokenException;
import org.ciallo.easytokencore.exception.EasyTokenInvalidException;
import org.ciallo.easytokencore.exception.response.ExceptionResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Easy-Token全局异常处理器
 *
 * <p>核心功能：</p>
 * <ul>
 *   <li>统一处理EasyToken相关的异常</li>
 *   <li>将异常转换为标准错误响应格式</li>
 *   <li>设置适当的HTTP状态码</li>
 * </ul>
 *
 * <p>异常处理说明：</p>
 * <ol>
 *   <li>处理EasyTokenInvalidException异常，返回401未授权状态</li>
 *   <li>返回统一的错误响应格式ExceptionResponse</li>
 *   <li>保留原始错误码和错误信息</li>
 * </ol>
 *
 * @Description: Easy-Token异常处理器
 * @Author: Jukomu
 * @Package: org.ciallo.easytokencore.exception.handler
 * @Project: easy-token-demo
 * @Version: 1.0.0
 * @name: EasyTokenExceptionHandler
 * @Date: 2025-05-07
 * @Filename: EasyTokenExceptionHandler
 * @see EasyTokenInvalidException
 * @see ExceptionResponse
 */
@RestControllerAdvice
public class EasyTokenExceptionHandler {
    @ExceptionHandler(EasyTokenException.class)
    public ExceptionResponse handleEasyTokenException(EasyTokenException e) {
        return ExceptionResponse.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(EasyTokenInvalidException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ExceptionResponse handleEasyTokenInvalidException(EasyTokenInvalidException e) {
        return ExceptionResponse.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(EasyLoginException.class)
    public ExceptionResponse handleEasyLoginException(EasyLoginException e) {
        return ExceptionResponse.fail(e.getCode(), e.getMessage());
    }
}
